import React, { Component } from 'react'
import PropTypes from 'prop-types'

export default class Child extends Component {
  // 默认值，只有父组件没有传递的时候，才起作用
  static defaultProps = {
    count: 200
  }

  // 文档地址：https://zh-hans.react.dev/reference/react/Component#static-proptypes
  static propTypes = {
    count: PropTypes.number
  }

  constructor (props) {
    super()

    this.state = {
      myCount: props.count
    }
  }

  change = () => {
    // 不允许这么做
    // this.props.count++
    // this.props.callback(this.props.count + 1)

    this.setState({
      myCount: this.state.myCount + 1
    })
  }

  render () {
    return (
      <div>
        Child
        {/* <p>{this.props.count}</p>
        <button onClick={this.change}>+1</button> */}
        <p>{this.state.myCount}</p>
        <button onClick={this.change}>+1</button>
      </div>
    )
  }
}
